package yue.shen.android.demo.select

import android.graphics.Bitmap
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.BitmapImageViewTarget
import com.luck.picture.lib.entity.LocalMediaFolder
import yue.shen.android.demo.R


class SelectFolderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    private val imageView = itemView.findViewById<ImageView>(R.id.image_view)
    private val textView = itemView.findViewById<TextView>(R.id.text_view)

    fun bindData(data: LocalMediaFolder) {
        val options: RequestOptions = RequestOptions()
            .placeholder(R.drawable.ic_placeholder)
            .centerCrop()
            .sizeMultiplier(0.5f)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .override(160, 160)
        Glide.with(itemView.context)
            .asBitmap()
            .load(data.firstImagePath)
            .apply(options)
            .into(object : BitmapImageViewTarget(imageView) {
                override fun setResource(resource: Bitmap?) {
                    val circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(itemView.resources, resource)
                    circularBitmapDrawable.cornerRadius = 8f
                    imageView.setImageDrawable(circularBitmapDrawable)
                }
            })
        textView.text = "${data.name} (${data.imageNum})"
    }
}